package com.iterator.service.wechat.wy.address;

import com.iterator.bean.wechat.wy.address.AddressBO;
import com.iterator.bean.wechat.wy.address.AddressDetailVO;
import com.iterator.bean.wechat.wy.address.AddressTemp;
import com.iterator.bean.wechat.wy.address.AddressVO;
import com.iterator.mapper.wechat.wy.address.WxAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Silence_W
 * @date: 2021-01-12 17:45
 **/
@Service
public class WxAddressServiceImpl implements WxAddressService{

    @Autowired
    WxAddressMapper wxAddressMapper;

    @Override
    public List<AddressVO> queryAddress(String username) {
        Integer integer = wxAddressMapper.selectId(username);
        List<AddressTemp>  addressVOs= wxAddressMapper.queryAddress(integer);
        List<AddressVO> addressVOS = new ArrayList<>();
        for (AddressTemp addressVO : addressVOs) {
            String s = detaileAddress(addressVO);
            addressVOS.add(new AddressVO(addressVO.getIsDefault(),s,addressVO.getName(),addressVO.getMobile(),addressVO.getId()));
        }

        return addressVOS;
    }

    @Override
    public AddressDetailVO queryAddressDetailById(Integer id) {
        AddressDetailVO addressDetailVO= wxAddressMapper.queryAddressDeatilById(id);
        String province = wxAddressMapper.queryRegion(addressDetailVO.getProvince_id());
        String city = wxAddressMapper.queryRegion(addressDetailVO.getCity_id());
        String area = wxAddressMapper.queryRegion(addressDetailVO.getArea_id());
        addressDetailVO.setProvinceName(province);
        addressDetailVO.setCityName(city);
        addressDetailVO.setAreaName(area);
        return addressDetailVO;
    }

    @Override
    public void updateAddressDetail(AddressBO addressBO) {
        if (addressBO.getIsDefault()){
            Boolean b1=true;
            Boolean b2=false;
            wxAddressMapper.updateDefault(b1,b2);

        }
        if (addressBO.getProvinceId()==null||addressBO.getCityId()==null||addressBO.getAreaId()==null){
            wxAddressMapper.updateAddressDetailById(addressBO);
                return ;
        }

       wxAddressMapper.updateAddressDetail(addressBO);


    }

    @Override
    public void deleteById(Integer id) {
            wxAddressMapper.deleteById(id);
    }

    @Transactional
    @Override
    public int insertAddress(AddressBO addressBO,String username) {
        Integer userId= wxAddressMapper.selectId(username);
        Boolean b1=true;
        Boolean b2=false;
        if (addressBO.getIsDefault()){

            wxAddressMapper.updateDefault(b1,b2);

        }
        wxAddressMapper.insertAddress(addressBO,userId,new Date(),b2);
        Integer id= wxAddressMapper.selectAddressId();
        return id;
    }

    public String detaileAddress(AddressTemp addressTemp){

        String province = wxAddressMapper.queryRegion(addressTemp.getProvince_id());
        String city = wxAddressMapper.queryRegion(addressTemp.getCity_id());
        String area = wxAddressMapper.queryRegion(addressTemp.getArea_id());
        return province+city+area+addressTemp.getAddress();
    }

}
